package pqt;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.List;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class Error extends JDialog {

	private final JPanel contentPanel = new JPanel();
	
	
	List lista = new List();
	
	private Bloque bloque;
	
	private JList list;
	
	private String item;
	
	private int indexItem;
	
	JButton btnCancelar = new JButton("");
	
	final JButton btnAceptar = new JButton("");
	
	public static void main(String[] args) {
		try {
			//Atributo dialog = new Atributo();
			//dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Error(Bloque bloque, int indexItem, String item) {
		
		this.bloque = bloque;
		this.item = item;
		this.indexItem = indexItem;
		
		for(int i=0;i<bloque.getFallaPosiblesSize();i++)
		lista.add(bloque.getFallaPosible(i));
		
		mostrarAtributos();
	}
	
	public void mostrarAtributos() {
		
		
		
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		
		int altoPantalla = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
		int anchoPantalla = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
		int anchoFrame = (int)(anchoPantalla*0.6);
		int altoFrame = (int)(altoPantalla*0.77);
		setBounds(0,0,761,522);
		setBounds(300, altoPantalla-altoFrame-40,anchoFrame,altoFrame);
		
		setUndecorated(true);
		
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(240, 240, 240));
		contentPanel.setBorder(new TitledBorder(new LineBorder(new Color(53, 85, 134), 2), item.toUpperCase(), TitledBorder.LEFT, TitledBorder.TOP, null, null));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		list = new JList(createLista(lista));
		list.setFont(new Font("Tahoma", Font.PLAIN, 17));
		final JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setBounds(10, 59, anchoFrame-20, 375);
		contentPanel.add(scrollPane);
		list.setCellRenderer(new CheckListRenderer());
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setBorder(new EmptyBorder(0, 4, 0, 0));
		list.setFocusable(false);
		
		
		btnAceptar.setFocusable(true);
		btnAceptar.setContentAreaFilled(false);
		btnAceptar.setBorderPainted(false);
		btnAceptar.setBorder(null);
		btnAceptar.setBounds(70, 500, 265, 75);
		btnAceptar.setIcon(new ImageIcon(Atributo.class.getResource("/img/AceptarNegroChico.png")));
		btnAceptar.setPressedIcon(new ImageIcon(PuestaCero.class.getResource("/img/AceptarNegroChico.png")));
		
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				imprimirTodos();
				dispose();
			}
		});
		
		btnAceptar.addMouseListener(new MouseAdapter() {
					
			public void mouseEntered(MouseEvent arg0) {
				btnAceptar.setIcon(new ImageIcon(PuestaCero.class.getResource("/img/AceptarVerdeChico.png")));
				btnCancelar.setIcon(new ImageIcon(PuestaCero.class.getResource("/img/CancelarNegroChico.png")));
				getRootPane().setDefaultButton(btnAceptar);
						
			}
					
			
		});
				
		btnAceptar.addFocusListener(new FocusAdapter() {
				
			public void focusGained(FocusEvent arg0) {
				btnAceptar.setIcon(new ImageIcon(PuestaCero.class.getResource("/img/AceptarVerdeChico.png")));
				btnCancelar.setIcon(new ImageIcon(PuestaCero.class.getResource("/img/CancelarNegroChico.png")));
				getRootPane().setDefaultButton(btnAceptar);
				
				
			}
					
			public void focusLost(FocusEvent e) {
				btnAceptar.setIcon(new ImageIcon(PuestaCero.class.getResource("/img/AceptarNegroChico.png")));
				
			}
		});
		contentPanel.add(btnAceptar);
		
		
		btnCancelar.setFocusable(true);
		btnCancelar.setContentAreaFilled(false);
		btnCancelar.setBorderPainted(false);
		btnCancelar.setBorder(null);
		btnCancelar.setBounds(360, 500, 275, 75);
		btnCancelar.setIcon(new ImageIcon(Atributo.class.getResource("/img/CancelarNegroChico.png")));
		btnCancelar.setPressedIcon(new ImageIcon(PuestaCero.class.getResource("/img/CancelarNegroChico.png")));
		
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		btnCancelar.addMouseListener(new MouseAdapter() {
					
			public void mouseEntered(MouseEvent arg0) {
				btnCancelar.setIcon(new ImageIcon(PuestaCero.class.getResource("/img/CancelarRojoChico.png")));
				btnAceptar.setIcon(new ImageIcon(PuestaCero.class.getResource("/img/AceptarNegroChico.png")));
				getRootPane().setDefaultButton(btnCancelar);
						
			}
					
			
		});
				
		btnCancelar.addFocusListener(new FocusAdapter() {
				
			public void focusGained(FocusEvent arg0) {
				btnCancelar.setIcon(new ImageIcon(PuestaCero.class.getResource("/img/CancelarRojoChico.png")));
				btnAceptar.setIcon(new ImageIcon(PuestaCero.class.getResource("/img/AceptarNegroChico.png")));
				getRootPane().setDefaultButton(btnCancelar);
				
				
			}
					
			public void focusLost(FocusEvent e) {
				btnCancelar.setIcon(new ImageIcon(PuestaCero.class.getResource("/img/CancelarNegroChico.png")));
				
			}
		});
		contentPanel.add(btnCancelar);
		
		JButton btnMarcarTodas = new JButton("Marcar Todas");
		btnMarcarTodas.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				marcarTodas();
			}
		});
		btnMarcarTodas.setBounds(10, 445, 161, 23);
		contentPanel.add(btnMarcarTodas);
		
		JButton btnDescarcarTodas = new JButton("Descarcar Todas");
		btnDescarcarTodas.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desmarcarTodas();
			}
		});
		btnDescarcarTodas.setBounds(181, 445, 146, 23);
		contentPanel.add(btnDescarcarTodas);
		
		JLabel lblSeleccioneFallas = new JLabel("Seleccione Falla/s:");
		lblSeleccioneFallas.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblSeleccioneFallas.setHorizontalAlignment(SwingConstants.LEFT);
		lblSeleccioneFallas.setBounds(10, 21, 178, 27);
		contentPanel.add(lblSeleccioneFallas);
		
		list.addMouseListener(new MouseAdapter() {
		 public void mouseClicked(MouseEvent e) {
		   int index = list.locationToIndex(e.getPoint());
		   CheckableItem item = (CheckableItem) list.getModel()
		       .getElementAt(index);
		   item.setSelected(!item.isSelected());
		   Rectangle rect = list.getCellBounds(index, index);
		   list.repaint(rect);
		 }
		});
		//getContentPane().add(scrollPane, BorderLayout.CENTER);
			
		
		
		
		
		setModal(true);
		
		setVisible(true);
	}

	private CheckableItem[] createLista(List strs) {
	    int n = strs.countItems();
	    CheckableItem[] items = new CheckableItem[n];
	    for (int i = 0; i < n; i++) 
	      items[i] = new CheckableItem(strs.getItem(i).toString());
	    
	    return items;
	  } 
	
	private void imprimirTodos() {
		ListModel model = list.getModel();
		   int n = model.getSize();
		   for (int i = 0; i < n; i++) {
		     CheckableItem item = (CheckableItem) model.getElementAt(i);
		     if (item.isSelected()) {
		    	 
		    	 bloque.item[indexItem].addFalla(item.toString());
		    	 bloque.setEstado(false);
		    	 bloque.item[indexItem].setEstado(false);
		     }
		   }
	}
	
	private void marcarTodas() {
		ListModel model = list.getModel();
		   int n = model.getSize();
		   for (int i = 0; i < n; i++) {
			   CheckableItem item = (CheckableItem) model.getElementAt(i);
			   item.setSelected(true);
			   Rectangle rect = list.getCellBounds(i, i);
			   list.repaint(i);
		   }
	}
	
	private void desmarcarTodas() {
		ListModel model = list.getModel();
		   int n = model.getSize();
		   for (int i = 0; i < n; i++) {
			   CheckableItem item = (CheckableItem) model.getElementAt(i);
			   item.setSelected(false);
			   Rectangle rect = list.getCellBounds(i, i);
			   list.repaint(i);
		   }
	}
}